//
//  GYZSuperFreeViewController.m
//  Qyer
//
//  Created by GYz on 15/10/21.
//  Copyright © 2015年 GF. All rights reserved.
//

#import "GYZSuperFreeViewController.h"
#import "BaseHeader.h"
@interface GYZSuperFreeViewController () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, copy)NSString *finalUrl;
@property(nonatomic, assign)NSInteger myPage;
@property (nonatomic, strong)UISegmentedControl *segment;

@end

@implementation GYZSuperFreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBarHidden = YES;
    self.infoArr = [[NSMutableArray alloc]init];
    self.myPage = 1;
    
    [self creatNav];
    [self creatSegment];
    
    if (self.left) {
        _segment.selectedSegmentIndex = 0;
        [self afn:0 page:1 isUp:NO];
        
    }else{
        _segment.selectedSegmentIndex = 1;
        [self afn:1 page:1 isUp:NO];
    }

}

#pragma mark - 创建navigation
- (void)creatNav
{
    UIView *navViea = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    navViea.userInteractionEnabled  = YES;
    navViea.backgroundColor = [UIColor colorWithRed:252./255 green:252./255 blue:252./255 alpha:1];
    
    UIButton *backBut = [self creatButtonWithRect:CGRectMake(5, 30, 30, 25) imageStr:@"iconfont-31fanhui1" click:@selector(goback)];
    
    UILabel *label = [self creatLabelWithRect:CGRectMake(0, 24, SCREEN_WIDTH, 30) text:[NSString stringWithFormat:@"%@特价折扣", self.cnname] font:[UIFont systemFontOfSize:18.0] textColor:[UIColor blackColor] alignment:YES];
    [navViea addSubview:label];
    
    [navViea addSubview:backBut];
    [self.view addSubview:navViea];
}

- (void)goback
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建segment
-(void)creatSegment
{
    NSArray *nameArr = @[@"超值自由行",@"精彩当地游"];
    _segment = [[UISegmentedControl alloc]initWithItems:nameArr];
    [_segment setFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT/20)];
    // 默认选择下标为几的第几个按钮
    [_segment setSelectedSegmentIndex:0];
    //设置选中的分段颜色
    [_segment setTintColor:[UIColor colorWithRed:34./255 green:169./ 255 blue:118./255 alpha:1]];
    //添加点击事件
    [_segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segment];
}

- (void)segmentClick:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 0)
    {
        self.myPage = 1;
        [self afn:seg.selectedSegmentIndex page:1 isUp:NO];
        [self.tableView setContentOffset:CGPointMake(0, 0)];
    }
    else
    {
        self.myPage = 1;
        [self afn:seg.selectedSegmentIndex page:1 isUp:NO];
        [self.tableView setContentOffset:CGPointMake(0, 0)];
    }
}

#pragma mark - 网络请求
- (void)afn:(NSInteger)number page:(NSInteger)page isUp:(BOOL)up
{
    if (number == 0)
    {
        _finalUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&order=2&page=%ld&track_app_version=6.6.6&track_user_id=6695170&type=%d&v=1",self.urlStr,page,self.type];
    }else
    {
        _finalUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&order=2&page=%ld&product_type=2410&time=1&track_app_version=6.6.6&track_user_id=6695170&type=%d&v=1",self.urlStr,page,self.type];
    }
    
    [AFN getdataWithString:_finalUrl block:^(id block) {
        
        if (!up) {
            self.infoArr = [[NSMutableArray alloc] init];
        }
        
        for (NSDictionary *dic in [[block objectForKey:@"data"] objectForKey:@"list"]) {
            GYZFreeModel *model = [GYZFreeModel modelWithDictionary:dic];
            [self.infoArr addObject:model];
        }
        if (self.tableView == nil) {
            [self creatTableView];
        }
        
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        
    } BLOCK:^(id BLOCK) {
    }];
}

#pragma mark - 加载
- (void)allData
{
    __block GYZSuperFreeViewController *freeC = self;
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        freeC.myPage += 1;
        if (_segment.selectedSegmentIndex == 0) {
            [freeC afn:0 page:freeC.myPage isUp:YES];
        }else{
            [freeC afn:1 page:freeC.myPage isUp:YES];
        }
    }];
}


#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64+SCREEN_HEIGHT/20, SCREEN_WIDTH, SCREEN_HEIGHT - 64-SCREEN_HEIGHT/20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self allData];
}

#pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.infoArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cellId";
    GYZFreeTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[GYZFreeTabelViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    GYZFreeModel *model = self.infoArr[indexPath.row];
    [cell setValueWithModel:model];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH * 3 / 12 + 20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GYZFreeModel *model = self.infoArr[indexPath.row];
    WebViewController *web = [[WebViewController alloc] init];
    web.webString = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.6.6&track_deviceid=301FBCE0-A789-4EAC-ADF1-95E1AACF2A70&track_user_id=6695170",model.cid];
    [self.navigationController pushViewController:web animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
